import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Robot } from './robot.entity';

@Injectable()
export class RobotService {
  constructor(
    @InjectRepository(Robot)
    private robotRepository: Repository<Robot>,
  ) { }

  async findAll(): Promise<Robot[]> {
    const robots = await this.robotRepository.find();

    // 临时解决方案：为所有机器人添加管理员权限
    return robots.map(robot => ({
      ...robot,
      role: 'admin'  // 暂时将所有机器人的角色设为admin
    }));
  }

  async findOne(id: number): Promise<Robot> {
    return this.robotRepository.findOne({ where: { id } });
  }

  async create(createRobotDto: any): Promise<Robot> {
    const robot = new Robot();
    robot.name = createRobotDto.name;
    robot.webhook = createRobotDto.webhook;
    robot.description = createRobotDto.description || '';

    return this.robotRepository.save(robot);
  }

  async update(id: number, updateRobotDto: any): Promise<Robot> {
    await this.robotRepository.update(id, updateRobotDto);
    return this.robotRepository.findOne({ where: { id } });
  }

  async remove(id: number): Promise<void> {
    await this.robotRepository.delete(id);
  }
} 